package com.davinci.freemarket.dataAccessLayer.accessdata;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;


/**
 * 
 * */
public class ConnectMSSQLServer
{
	/**
	 * 
	 */
	protected Logger log;
	
	/**
	 * 
	 */
	private static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ;
	
	
	
	//--------------------------
	// CONSTRUCTOR
	//--------------------------
	public ConnectMSSQLServer()
	{
//		ConnectMSSQLServer connServer = new ConnectMSSQLServer();
//		connServer.dbConnect("jdbc:sqlserver://localhost:1433", "","");

		configureLogger();
		log.info("\nInstancia de ConnectMSSQLServer creada");
	}

	protected void configureLogger(){
		//--- usgin logger
		this.log = Logger.getLogger(ConnectMSSQLServer.class.getClass().getName());
		BasicConfigurator.configure();
		this.log.setLevel(Level.ALL);
		log.info("\nLoguer configurado");
	}

	
	public static void main(String[] args)
	{
		ConnectMSSQLServer connServer = new ConnectMSSQLServer();
		String datos = "localhost:1433;databaseName=Intranet;";
//		String datos = "MUMA-MACHINE/SQLEXPRESS;databaseName=dvproject;integratedSecurity=true;";
//		String datos = "localhost:1433;databaseName=Pullman;integratedSecurity=true;";
		
		/*
		connServer.dbConnect("jdbc:sqlserver://localhost:1433;databaseName=Pullman;integratedSecurity=true;", "","");*/
		
		try {
			Class.forName(DRIVER);
			Connection conn = DriverManager.getConnection("jdbc:sqlserver://"+ datos, "Muma", "muma");
			System.out.println("connected");
			Statement statement = conn.createStatement();
			String queryString = "select * from sysobjects where type='u'";
			ResultSet rs = statement.executeQuery(queryString);
			while (rs.next()) {
				System.out.println(rs.getString(1));
			}
		} catch (Exception e) {
//			log.error("\nError al conectar a la base de datos");
			e.printStackTrace();
		}
	}


	/**
	 * 
	 */
	public void dbConnect(String db_connect_string, String db_userid, String db_password) {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
			System.out.println("connected");
			Statement statement = conn.createStatement();
			String queryString = "select * from sysobjects where type='u'";
			ResultSet rs = statement.executeQuery(queryString);
			while (rs.next()) {
				System.out.println(rs.getString(1));
			}
		} catch (Exception e) {
			log.error("\nError al conectar a la base de datos");
			e.printStackTrace();
		}
	}


}